package com.design.graduation.asdemo.utils;

import android.content.Context;
import android.os.Environment;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;

/**
 * Created by liyugang on 9/2/15.
 */
public class HSFFileUtils {

    public static String getCaptureImageUrl(Context context){
        // External sdcard location
        File mediaStorageDir = context.getExternalFilesDir(Environment
                .DIRECTORY_PICTURES);

        // Create the storage directory if it does not exist
        assert mediaStorageDir != null;
        if (!mediaStorageDir.exists()) {
            if (!mediaStorageDir.mkdirs()) {
                LogUtils.d("Oops! Failed create image directory");
                return null;
            }
        }
        return mediaStorageDir.getPath();


    }

    public static String getAudioUrl(Context context){
        File audioFile = context.getExternalFilesDir(Environment
                .DIRECTORY_ALARMS);
        assert audioFile != null;
        if (!audioFile.exists()){
            if (!audioFile.mkdirs()) {
                LogUtils.d("Oops! Failed create alarms directory");
                return null;
            }
        }
        return audioFile.getPath();
    }

    public static byte[] audioToBytes(String filePath) {
        try {
            File tmpFile = new File(filePath);
            BufferedInputStream in = new BufferedInputStream(
                    new FileInputStream(tmpFile));
            ByteArrayOutputStream baos = new ByteArrayOutputStream();

            byte[] buffer = new byte[in.available()];
            int length;
            while ((length = in.read(buffer)) != -1) {
                baos.write(buffer, 0, length);
            }
            byte[] bytes = baos.toByteArray();
            return bytes;
        } catch (Exception e) {
            LogUtils.e("读取音频出现异常"+ e);
        }
        return null;
    }
}
